package com.hydl.power.service.impl;

import com.hydl.power.mapper.InspectionContentMapper;
import com.hydl.power.mapper.InspectionOrderMapper;
import com.hydl.power.mapper.InspectionplanMapper;
import com.hydl.power.mapper.MaintainerMapper;
import com.hydl.power.pojo.InspectionContent;
import com.hydl.power.pojo.InspectionOrder;
import com.hydl.power.pojo.Inspectionplan;
import com.hydl.power.pojo.Maintainer;
import com.hydl.power.service.InspectionContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InspectionContentServiceImpl implements InspectionContentService {


    @Autowired
    private InspectionContentMapper inspectionContentMapper;

    @Autowired
    private InspectionOrderMapper inspectionOrderMapper;

    @Autowired
    private InspectionplanMapper inspectionplanMapper;

    @Autowired
    private MaintainerMapper maintainerMapper;

    @Override
    public List<InspectionContent> getInspectionContent() {
        List<InspectionContent> inspectionContent = inspectionContentMapper.getInspectionContent();
        List<InspectionContent> list = getChild(inspectionContent, 0);
        return list;
    }

    public  List<InspectionContent> getChild(List<InspectionContent> inspectionContentList , Integer pid){
        List<InspectionContent> list=new ArrayList<>();
        for (int i=0;i<inspectionContentList.size();i++){
            InspectionContent inspectionContent = inspectionContentList.get(i);
            if (inspectionContent.getPid()==pid){
                list.add(inspectionContent);
                List<InspectionContent> child = getChild(inspectionContentList, inspectionContent.getId());
                inspectionContent.setList(child);
            }
        }
        return list;
    }


    @Override
    public int updateInspectionOrder(Integer orderId ,String result,String IPQCID,String maintainerImage) {
      Inspectionplan inspectionplan = inspectionplanMapper.queryplanById(orderId);
      InspectionOrder inspectionOrder= new InspectionOrder();
      //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      int flag = inspectionOrderMapper.queryIsFlag(orderId.toString());
      if(flag == 0){
        //巡查人员
        Maintainer maintainer = maintainerMapper.selectById1(IPQCID);
          if(inspectionplan !=null){
            inspectionOrder.setProjectId(inspectionplan.getProjectId());
            inspectionOrder.setProjectName(inspectionplan.getProjectName());
            inspectionOrder.setPoweroomId(inspectionplan.getRoomid());
            inspectionOrder.setPowerroomName(inspectionplan.getRoomname());
            inspectionOrder.setMaintainerid(inspectionplan.getMaintainerid());
            inspectionOrder.setMaintainerName(inspectionplan.getMaintainer());
            inspectionOrder.setResult(result);
            inspectionOrder.setStatus(1);
            inspectionOrder.setInspectionPlanId(inspectionplan.getTid());
            inspectionOrder.setIPQCID(maintainer.getMaintainerId());
            inspectionOrder.setIPQCNAME(maintainer.getName());
            inspectionOrder.setIPQCIphone(maintainer.getPhone());
            inspectionOrder.setMaintainerImage(maintainerImage);

              int i = inspectionplanMapper.deleteByPrimaryKey(orderId);
              if(i == 0){
                return 0;
              }
          }
        return inspectionOrderMapper.addInspectionResult(inspectionOrder);
      }
      Maintainer maintainer = maintainerMapper.selectById1(IPQCID);
      int i = inspectionOrderMapper.updateInspectionResult(result,maintainer.getMaintainerId(),maintainer.getName(),orderId.toString());
      if(i==1){
        return 1;
      }
        return 0;
    }

    @Override
    public List<InspectionContent> getInspectionResult(Integer id) {
        InspectionOrder inspectionOrder= inspectionOrderMapper.getInspectionResultByOrderId(id);
        if (inspectionOrder!=null){
            List<InspectionContent> list = getChild(inspectionContentMapper.getInspectionContent(), 0);
            String[] strArr = inspectionOrder.getResult().split(";");
            for (int i=0;i<strArr.length;i++){
                String[] arr = strArr[i].split("-");
                for (InspectionContent inspectionContent:list){
                    List childList = inspectionContent.getList();
                    for (int j=0;j<childList.size();j++){
                        InspectionContent inspectionContentChild = (InspectionContent)childList.get(j);
                        if (inspectionContentChild.getId().toString().equals(arr[0])){
                            inspectionContentChild.setValue(arr[1]);
                            if (arr[2].equals("0")){
                                inspectionContentChild.setContent(null);
                            }else {
                                inspectionContentChild.setContent(arr[2]);
                            }
                        }
                    }
                }
            }
            return list;
        }
        return null;
    }

    @Override
    public int updateUserSubmitImage(String inspectionReport,String inspectionPlanId) {
      int i = inspectionOrderMapper.updateUserSubmitImage(inspectionReport,inspectionPlanId);
      if(i==1){
        return 1;
      }
      return 0;
    }
}
